python stop daemon thread

35

# function that will be in a thread
def my_func(msg_queue, stop_event):
    while not stop_event.is_set(): # <============= will stop if stop_event is set
        msg_queue.put("hi")
        sleep(0.1)

stop_event= threading.Event()
p = Thread(target=my_func, args=(msg_queue, stop_event))
p.start()

stop_event.set() # <====== to stop the thread

Comments

Submit
0 Comments